package com.icetec.iceintel.ui.adapter;

import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import java.util.ArrayList;

import com.icetec.R;
import com.icetec.iceintel.database.AlarmConfigSqlHelper;
import com.icetec.iceintel.framework.AppApplication;
import com.icetec.iceintel.model.Alarm;
import com.icetec.iceintel.netrequest.InstrutionSender;
import com.icetec.iceintel.service.AlarmServiceBroadcastReciever;
import com.icetec.iceintel.ui.activity.AlarmModificationActivity;
import com.icetec.iceintel.utility.ToastUtility;


/**
 * Created by amo on 2015/3/13.
 * 睡眠闹钟适配器
 */
public class SleepAlarmAdapter extends BaseAdapter {


    private ArrayList<Alarm> alarmArrayList;
    LayoutInflater layoutInflater;
    AlarmConfigSqlHelper alarmConfigSqlHelper;
    InstrutionSender instrutionSender;
    public SleepAlarmAdapter(){

        layoutInflater=LayoutInflater.from(AppApplication.getShareInstance());
        alarmConfigSqlHelper=new AlarmConfigSqlHelper();
        alarmArrayList=alarmConfigSqlHelper.getAllAlarm(Alarm.AlarmType.SLEEP);
        instrutionSender=new InstrutionSender();
    }


    public void refreshData(){
        alarmArrayList=alarmConfigSqlHelper.getAllAlarm(Alarm.AlarmType.SLEEP);
        notifyDataSetChanged();
    }



    @Override
    public int getCount() {
        return alarmArrayList.size() ;
    }

    @Override
    public Object getItem(int position) {
        return alarmArrayList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

            convertView =layoutInflater.inflate(
                    R.layout.component_alarm_list_element, null);

        final  Alarm alarm = (Alarm) getItem(position);

        CheckBox checkBox = (CheckBox)  convertView.findViewById(R.id.checkBox_alarm_active);
        checkBox.setChecked(alarm.getAlarmActive());
        checkBox.setTag(position);
        //      checkBox.setOnClickListener(alarmActivity);
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                alarm.setAlarmActive(isChecked);
                if(isChecked){
                    ToastUtility.show(alarm.getTimeUntilNextAlarmMessage());
                    AlarmModificationActivity.allocatePort(alarm);
                    ToastUtility.show(R.string.alarm_is+alarm.getAlarmPort()+R.string.activated);
                    alarmConfigSqlHelper.update(alarm);

                }

                else{
                    ToastUtility.show(R.string.alarm_is+alarm.getAlarmPort()+R.string.cancelled);
                    alarmConfigSqlHelper.update(alarm);
                }

                instrutionSender.setAlarm(alarm);
                callMathAlarmScheduleService();
            }
        });
        TextView alarmTimeView = (TextView)  convertView
                .findViewById(R.id.textView_alarm_time);
        alarmTimeView.setText(alarm.getAlarmTimeString());


        TextView alarmDaysView = (TextView)  convertView
                .findViewById(R.id.textView_alarm_days);
        alarmDaysView.setText(alarm.getRepeatDaysString());


        return  convertView;
    }

    protected void callMathAlarmScheduleService() {

        Log.i("启动服务", "正在启动闹钟服务接收器");
        Intent mathAlarmServiceIntent = new Intent(AppApplication.getShareInstance(), AlarmServiceBroadcastReciever.class);
        AppApplication.getShareInstance().sendBroadcast(mathAlarmServiceIntent, null);

    }
}
